package androidvn.xenforo.fourums;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListFragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import androidvn.xenforo.fourums.R;
import androidvn.xenforo.fourums.adapter.CustomNodeListAdapter;
import androidvn.xenforo.fourums.model.Node;

public class ForumChildNodeFragment extends ListFragment {

	private ArrayList<Node> nodeList, forumChildNodeList, childList;
	private HashMap<String, ArrayList<Node>> forumChildChildNodeList;
	Node node = new Node();

	private String forumId;
	boolean loginStatus;
	// ALL JSON node names
	private static final String TAG_NODE_ID = "node_id";
	private static final String TAG_TITLE = "title";
	private static final String TAG_PARENT_NODE_ID = "parent_node_id";
	private static final String TAG_DISPLAY_ORDER = "display_order";
	private static final String TAG_DESCRIPTION = "description";
	private static final String TAG_DISPLAY_IN_LIST = "display_in_list";
	private static final String TAG_DEPTH = "depth";
	// private static final String TAG_BOX = "nameBox";

	setOnForumChildNodeClickListener communicator;

	@Override
	public void onAttach(Activity activity) {

		super.onAttach(activity);
		try {
			communicator = (setOnForumChildNodeClickListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement setOnForumClickListener");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.forum_node_list_layout,
				container, false);

		Bundle bundle = this.getArguments();
		if (bundle != null) {
			forumId = bundle.getString("nodeId");

			Log.d("On Forum Child Params >>>", "Forum ID: " + forumId + " / "
					+ " " + " - " + " " + " / Status: " + Global.loginStatus
					+ " - Username: " + Global.userName + " - Hash: "
					+ Global.userHash);
		}

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		nodeList = new ArrayList<Node>();
		forumChildNodeList = new ArrayList<Node>();
		childList = new ArrayList<Node>();
		forumChildChildNodeList = new HashMap<String, ArrayList<Node>>();

		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(getActivity());

		try {
			JSONObject jsonNodesRespond = new JSONObject(sp.getString(
					"jsonNodes", "[]"));
			Node node = null;

			Iterator<?> keys = jsonNodesRespond.keys();
			while (keys.hasNext()) {
				String key = (String) keys.next();
				JSONObject jsonNode = jsonNodesRespond.getJSONObject(key);
				node = new Node();
				node.setNodeId(jsonNode.getString(TAG_NODE_ID));
				node.setTitle(jsonNode.getString(TAG_TITLE));
				node.setParentNodeId(jsonNode.getString(TAG_PARENT_NODE_ID));
				node.setDisplayOrder(jsonNode.getString(TAG_DISPLAY_ORDER));
				node.setDescription(jsonNode.getString(TAG_DESCRIPTION));
				node.setDisplayInList(jsonNode.getString(TAG_DISPLAY_IN_LIST));
				node.setDepth(jsonNode.getString(TAG_DEPTH));
				nodeList.add(node);
			}
			// childList depth 3
			for (int i = 0; i < nodeList.size(); i++) {
				if (nodeList.get(i).getDepth().equals("3")
						&& nodeList.get(i).getDisplayInList().equals("1")) {
					childList.add(nodeList.get(i));
				}
			}
			for (int i = 0; i < nodeList.size(); i++) {
				if (nodeList.get(i).getParentNodeId().equals(forumId)) {
					forumChildNodeList.add(nodeList.get(i));
				}
				for (int j = 0; j < forumChildNodeList.size(); j++) {

					for (int j2 = 0; j2 < childList.size(); j2++) {
						if (forumChildNodeList.get(j).getNodeId()
								.equals(childList.get(j2).getParentNodeId())) {
							forumChildChildNodeList.put(
									forumChildNodeList.get(j).getNodeId(),
									childList);
						}
					}

				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		CustomNodeListAdapter adapter = new CustomNodeListAdapter(
				getActivity(), forumChildNodeList);
		setListAdapter(adapter);
		// on child forum item click
		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				// set action title
				getActivity().setTitle(
						forumChildNodeList.get(position).getTitle());

				// check it has child or not
				if (forumChildChildNodeList.get(forumChildNodeList
						.get(position).getNodeId()) != null) {
					communicator.onForumChildNodeClick(
							forumChildNodeList.get(position).getNodeId(), true);
				} else {
					communicator
							.onForumChildNodeClick(
									forumChildNodeList.get(position)
											.getNodeId(), false);
				}
			}
		});

	}

	public void setCommunicator(setOnForumChildNodeClickListener communicator) {
		this.communicator = communicator;
	}

	interface setOnForumChildNodeClickListener {
		public void onForumChildNodeClick(String nodeId, boolean haveChild);
	}

	public void getChildNodeId(String nodeId) {
		this.forumId = nodeId;
	}
}
